package org.delphy.indy2.containers
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.containers.Canvas;
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import org.delphy.indy2.core.ICarbonComponent;

	public class CarbonNotificationWindow extends Canvas implements ICarbonComponent
	{

		//--------------------------------------
		//  Animation Effects
		//--------------------------------------
		
		/**
		 * @public
		 * The duration of showing up/closing down effects (unit: s)
		 * @default 1.3
		 */
		public var transitionTime:Number=1.3;	//unit: s
		
		/**
		 * @public
		 * How much time will the window stays open (unit: ms)
		 * @default 3000
		 */
		public var delayTime:Number=3000;	//unit: ms
		
		/**
		 * @protected
		 */
		protected var timer:Timer;
		
		//--------------------------------------
		//  Position and spacing
		//--------------------------------------
		
		/**
		 * @public
		 * 
		 * @default center
		 */
		[Inspectable(type="List", enumeration="center,topLeft,topRight,bottomLeft,bottomRight", defaultValue="center")]
		public var position:String="center";	
		
		/**
		 * @public
		 * 
		 * @default 20
		 */
		public var space:Number=20;
		
		/**
		 * @public
		 */
		public static const POSITION_CENTER:String="center";

		/**
		 * @public
		 */
		public static const POSITION_TOP_LEFT:String="topLeft";

		/**
		 * @public
		 */
		public static const POSITION_TOP_RIGHT:String="topRight";
		
		/**
		 * @public
		 */
		public static const POSITION_BOTTOM_LEFT:String="bottomLeft";

		/**
		 * @public
		 */
		public static const POSITION_BOTTOM_RIGHT:String="bottomRight";
		
		/**
		 * Constructor
		 */
		public function CarbonNotificationWindow()
		{
			super();
			
			horizontalScrollPolicy="off";
			verticalScrollPolicy="off";
			
			visible=false;
		}
		
		/**
		 * @protected
		 */
		override protected function commitProperties() : void{
			super.commitProperties();
			
			if(!styleName)
				styleName="carbonNotificationWindow";
		}
		
		/**
		 * @public
		 */
		public function popUp():void{		

			if(Tweener.isTweening(this)){
				return;
			}
			
			alpha=0;			
			
			// Initialize position
			
			switch(position){
				case POSITION_CENTER:
					x=Application.application.width/2-width/2;
					y=Application.application.height/2-height/2;
					break;
				case POSITION_TOP_LEFT:
					x=space;
					y=space;
					break;
				case POSITION_TOP_RIGHT:
					x=Application.application.width-width-space;
					y=space;
					break;
				case POSITION_BOTTOM_LEFT:
					x=space;
					y=Application.application.height-height-space;
					break;
				case POSITION_CENTER:
					x=Application.application.width-width-space;
					y=Application.application.height-height-space;
					break;
			}
			
			if(parent)
				parent.removeChild(this);
			
			PopUpManager.addPopUp(this,DisplayObject(Application.application),false);
			
			if(!visible){
				visible=true;
			}
			
			Tweener.addTween(this,{alpha:1,time:transitionTime,onComplete:onPopUpAnimationComplete});
		}

		/**
		 * @protected
		 */
		protected function onPopUpAnimationComplete():void{
			timer=new Timer(delayTime);			
			timer.addEventListener(TimerEvent.TIMER,close);
			timer.start();
		}
		
		/**
		 * @public
		 */
		public function close(evt:Event=null):void{
			timer.stop();
			Tweener.addTween(this,{alpha:0,time:transitionTime,onComplete:onCloseAnimationComplete});
		}
		
		/**
		 * @protected
		 */
		protected function onCloseAnimationComplete():void{
			PopUpManager.removePopUp(this);
		}
		
		/**
		 * @public
		 */
		public function get defaultStyleName():String{
			return "carbonNotificationWindow";
		}
	}
}